package com.hzsoft.sys.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.hzsoft.base.entity.BJuiJSON;
import com.hzsoft.base.entity.Page;
import com.hzsoft.sys.entity.SysUser;
import com.hzsoft.sys.entity.TblQuestionBank;
import com.hzsoft.sys.entity.TblScoreManagement;
import com.hzsoft.sys.entity.TblTestParameter;
import com.hzsoft.sys.entity.TblTestTemp;
import com.hzsoft.sys.service.TblQuestionBankService;
import com.hzsoft.sys.service.TblScoreManagementService;
import com.hzsoft.sys.service.TblTestParameterService;
import com.hzsoft.sys.service.TblTestTempService;
import com.hzsoft.util.Const;
import com.hzsoft.util.DateUtil;
import com.hzsoft.util.JsonUtil;

/**
 * 答题
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/answer/test")
public class TblTestController {
	@Autowired
	private TblQuestionBankService tblQuestionBankService;
	@Autowired
	private TblTestParameterService parameterService;
	@Autowired
	private TblScoreManagementService managementService;
	@Autowired
	private TblTestTempService tempService; 
	
	@RequestMapping("/testList")
	public ModelAndView showUser(@ModelAttribute TblTestParameter test,Model model,HttpServletRequest request,HttpServletResponse response) {
		//分页设置
		String pageCurrent = request.getParameter("pageCurrent");
		Page page =  new Page();
		if (StringUtils.isNotBlank(pageCurrent)) {
			page.setPageCurrent(Integer.parseInt(pageCurrent));
			test.setPage(page);
		}
		
		List<TblTestParameter> list = parameterService.listPageTest(test);
		
		model.addAttribute("test",test);
		model.addAttribute("page", test.getPage());
		model.addAttribute("list",list);
		
		return new ModelAndView("answer/test/testList");
		
	}
	
	/**
	 * 生成试卷
	 * @param bank
	 * @param test
	 * @param testid
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping("/toTest")
	public ModelAndView prepareAdd(@ModelAttribute TblQuestionBank bank,TblTestParameter test,Model model,String testid,HttpServletResponse response, HttpServletRequest request){
		System.out.println("---------testid---"+testid);//试卷的id
		//获取当前的答题用户
		String userId=((SysUser)request.getSession().getAttribute(Const.SESSION_USER)).getUserId();		
		
		
		//1.根据参数设置获取单选题id，多选题Id，判断题id
		
		TblTestParameter testList = parameterService.selectByPrimaryKey(Integer.parseInt(testid));
		
		
		List sList = tblQuestionBankService.listId("1");//单选
		List mList = tblQuestionBankService.listId("2");//多选
		List pList = tblQuestionBankService.listId("3");//判断
		Collections.shuffle(sList);
		Collections.shuffle(mList);
		Collections.shuffle(pList);
		
		List<TblQuestionBank> newlist = new ArrayList();
		
		List<TblQuestionBank> newSlist = new ArrayList();
		List<TblQuestionBank> newMlist = new ArrayList();
		List<TblQuestionBank> newPlist = new ArrayList();
		List<TblScoreManagement> scorelist = new ArrayList();
		List<TblTestTemp> templist = new ArrayList();
		TblScoreManagement management = new TblScoreManagement();
		
		
		/**
		 * 随机取单选题的数量
		 */
        if(sList.size() >= testList.getSinglesl()) {
        	//如果list里面剩下的还大于等于需要取出的  
            for(int n = 0; n < testList.getSinglesl(); n++) {//则重复执行size次下面的两句代码  
            	newSlist.add((TblQuestionBank) sList.get(0));  
                sList.remove(0);  
            }  
        } 
		/**
		 * 随机取duo选题的数量
		 */
		 if(mList.size() >= testList.getMultiple()) {
	        	//如果list里面剩下的还大于等于需要取出的  
	            for(int n = 0; n < testList.getMultiple(); n++) {//则重复执行size次下面的两句代码  
	            	newMlist.add((TblQuestionBank) mList.get(0));  
	            	mList.remove(0);  
	            }  
	        } 
		/**
		 * 随机取判断题的数量
		 */
		 if(pList.size() >= testList.getPdtsl()) {
	        	//如果list里面剩下的还大于等于需要取出的  
	            for(int n = 0; n < testList.getPdtsl(); n++) {//则重复执行size次下面的两句代码  
	            	newPlist.add((TblQuestionBank) pList.get(0));  
	            	pList.remove(0);  
	            }  
	        } 
		 
		 /**
		  * 把三个list合并成一个list 取出id 放入templist中
		  */
		 newlist.addAll(newSlist);
		 newlist.addAll(newMlist);
		 newlist.addAll(newPlist);
		 
		 for (TblQuestionBank b : newlist) {
			 TblTestTemp testTemp = new TblTestTemp();
			 testTemp.setQid(b.getId()+"");
			 testTemp.setParid(testid);
			 testTemp.setUserid(userId);
			 testTemp.setCoun("1");
			 templist.add(testTemp);
		}
		 
		 System.out.println("-----all-----"+JSON.toJSONString(templist));
		
	 	management.setRespondents(userId);
		management.setTestid(testid);
		management.setQuiztime(DateUtil.getCurrentDate());
		management.setStartTime(DateUtil.getCurrentDateAndTime());
		
//		managementService.insertSelective(management);//建成绩单
//		tempService.addTemp(templist);
		
		model.addAttribute("toutaltime",testList.getTime());//用户id
		model.addAttribute("coun","1");//用户id
		model.addAttribute("userId",userId);//用户id
		model.addAttribute("testid",testid);//试卷id 
		model.addAttribute("newSlist",newSlist);
		model.addAttribute("newMlist",newMlist);
		model.addAttribute("newPlist",newPlist);
		
		return new ModelAndView("answer/test/test");
	}
	
	/**
	 * 动态设置 答题是否正确
	 * @param bank
	 * @param test
	 * @param id
	 * @param flag
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/ajaxFlag")
	public void ajaxFlag(@ModelAttribute TblQuestionBank bank,TblTestTemp test,HttpServletRequest request, HttpServletResponse response) throws IOException{
		System.out.println("--------------");
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		int data=1;
		String id = request.getParameter("id");//试题id
		String testid = request.getParameter("testid");//试卷id
		String flag = request.getParameter("flag");//是否正确
		String userId = request.getParameter("userId");
		String coun = request.getParameter("coun");//答题次数
		System.out.println("----"+id+"----"+testid+"----"+flag+"----"+coun+"----"+userId);
		
		test.setAnswer(flag);
		test.setParid(testid);
		test.setCoun(coun);
		test.setQid(id);
		test.setUserid(userId);
		
		int count = tempService.updateByUserId(test);
		if(count >0){
			data=1;
		}else{
			data=0;
		}
		out.print(data);
	}
	
	
}
